package com.zxh.zhenxihuan.controller;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;


import com.zxh.zhenxihuan.mapper.ShangjiaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.swing.text.html.FormSubmitEvent;
import java.io.File;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
@RequestMapping("/sh")
public class SMSController {
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Autowired
    ShangjiaMapper shangjiaMapper;

    @Resource
    private JavaMailSender javaMailSender;
    //短信接口
    @PostMapping("/send_sms")
    public Boolean sendMsg(
            @RequestBody Map<String,Object> map){
        String tel = (String)map.get("tel");
        System.out.println("===>"+tel);
        DefaultProfile profile = DefaultProfile.getProfile(
                "https://signin.aliyun.com/1826941471512099.onaliyun.com/login.htm",
                "LTAIFnFfMOELnxj7",
                "iYph2N1Gs8VCiUybhn8tgwaHG2mnBA");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("SignName", "java58kehai");
        request.putQueryParameter("TemplateCode", "SMS_172010707");
        int code=(int)((Math.random()*9+1)*100000);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            // redis存放验证码
            redisTemplate.opsForValue().set(tel,code+"",180, TimeUnit.SECONDS);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return true;
    }

    //商家添加
    public int shangJiaReg(String name,String tel,String pwd,String yzm){
        //从redis数据库获得验证码
        boolean ysmIsRight= redisTemplate.opsForValue().get("tel").equals(yzm);
        return 1;
    }
    //查询电话重复
    @RequestMapping("/is_tel_repeat")
    public int is_tel_repeat(String tel){
        return shangjiaMapper.is_tel_repeat(tel);
    }

    //邮件发送
    @GetMapping("/send_mail")
    public void sendMail(){
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("524530946@qq.com");
            helper.setTo("787231806@qq.com");
            helper.setSubject("标题：你是如此优秀");

            StringBuffer sb = new StringBuffer();
            sb.append("<h1>帅的嘛，不谈哦</h1>")
                    .append("<p style='color:#F00'>走位，走位</p>")
                    .append("<p style='text-align:right'>这波怎么说</p>");
            helper.setText(sb.toString());
//            FileSystemResource fileSystemResource=new FileSystemResource(new File("E://t//dmxy.jpg"));
//            helper.addAttachment("德玛西亚正义",fileSystemResource);
            javaMailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
}
